//delete//
const Utils = require('../util/utils');
const statusCode = require('../util/status-code')
const getCardList = require("../mock/server/getCardList");
const getListByPageIdAndName = require("../mock/server/getListByPageIdAndName");
const getRefreshCard = require("../mock/server/getRefreshCard");
const getRefreshCardFour = require("../mock/server/getRefreshCardFour");
const getCardTableDisplay = require("../mock/server/getCardTableDisplay");
const getHeatMapPerData = require("../mock/server/getHeatMapPerData");
const xlsx = require('node-xlsx');

//delete//
class BuryPointCardController {
    /**
     * 创建信息
     * @param ctx
     * @returns {Promise.<void>}
     */
    static async create(ctx) {
        let req = JSON.parse(ctx.request.body);
        if (req.name && req.type && req.pageId && req.calcRule) {
            ctx.response.status = 200;
            ctx.body = statusCode.SUCCESS_200('创建卡片成功', '')
        } else {
            ctx.response.status = 412;
            ctx.body = statusCode.ERROR_412('创建卡片失败，请求参数不能为空！')
        }
    }
  
    static async delete(ctx) {
        let param = JSON.parse(ctx.request.body);
        const { id } = param
        // 删除前，先检查所有统计数据是否删除
        ctx.response.status = 200;
        ctx.body = statusCode.SUCCESS_200('success', "")
    }

    /**
     * 查询所有卡片列表
     * @param {}} ctx 
     */
    static async getList(ctx) {
        ctx.response.status = 200;
        ctx.body = statusCode.SUCCESS_200('success', getCardList)
    }
    /**
     * 查询所有卡片列表
     * @param {}} ctx 
     */
    static async getListByPageIdAndName(ctx) {
        ctx.response.status = 200;
        ctx.body = statusCode.SUCCESS_200('success', getListByPageIdAndName)
    }

    /**
     * 刷新
     * @param ctx
     * @returns {Promise.<void>}
     */
    static async refresh(ctx) {
        let req = JSON.parse(ctx.request.body);
        if (req.id) {
            if(req.id === '1' || req.id === 1){
                let todayDate = new Date().Format("MM-dd");
                let refreshCardInfo = getRefreshCard;
                let calcData = refreshCardInfo.statisticList[0].calcData;
                calcData[calcData.length-1].happenDate = todayDate;
                calcData[calcData.length-1].count = Math.floor((Math.random()*1000000)+1000000);

                ctx.response.status = 200;
                ctx.body = statusCode.SUCCESS_200('卡片刷新成功',refreshCardInfo)
            }else if(req.id === '2' || req.id === 2){
                let refreshCardInfo = getRefreshCardFour;
                ctx.response.status = 200;
                ctx.body = statusCode.SUCCESS_200('卡片刷新成功',refreshCardInfo)
            }else{
                ctx.response.status = 200;
                ctx.body = statusCode.SUCCESS_200('卡片刷新成功', '')
            }
        } else {
            ctx.response.status = 412;
            ctx.body = statusCode.ERROR_412('卡片刷新失败！')
        }
    }
    /**
     * 排序
     * @param ctx
     * @returns {Promise.<void>}
     */
    static async sort(ctx) {
        let req = JSON.parse(ctx.request.body);
        if (req) {    
            ctx.response.status = 200;
            ctx.body = statusCode.SUCCESS_200('置顶成功！', '');
        } else {
            ctx.response.status = 412;
            ctx.body = statusCode.ERROR_412('置顶失败！')
        }
    }

    /**
     * 导出卡片表格形式
     * @param ctx
     * @returns {Promise.<void>}
     */
    static async tableDisplay(ctx){
        ctx.response.status = 200;
        ctx.body = statusCode.SUCCESS_200('置顶成功！', getCardTableDisplay);
    }

     /**
     * 导出卡片表格形式
     * @param ctx
     * @returns {Promise.<void>}
     */
    static async export(ctx) {
        const param = Utils.parseQs(ctx.request.url)
        var dataList = [["日期","用户总数","老用户数量","新用户数量"],["2022-03-22",296,48,48],["2022-03-23",316,53,53],["2022-03-24",301,63,63],["2022-03-25",289,47,47],["2022-03-26",188,25,25],["2022-03-27",162,18,18],["2022-03-28",342,75,75],["2022-03-29",307,55,55],["2022-03-30",257,46,46],["2022-03-31",332,61,61],["2022-04-01",187,39,39],["2022-04-02",274,48,48],["2022-04-03",160,22,22],["2022-04-04",126,14,14],["2022-04-05",115,10,10],["2022-04-06",293,50,50],["2022-04-07",358,73,73],["2022-04-08",343,64,64],["2022-04-09",187,21,21],["2022-04-10",139,17,17],["2022-04-11",302,47,47],["2022-04-12",325,67,67],["2022-04-13",343,64,64],["2022-04-14",323,82,82],["2022-04-15",325,59,59],["2022-04-16",175,16,16],["2022-04-17",213,21,21],["2022-04-18",329,78,78],["2022-04-19",348,73,73],["2022-04-20",192,45,45]]; //这是第一行 俗称列名
        const buffer = xlsx.build([{name: 'sheet1', data: dataList}]); 
        const fileSize = buffer.length;
        
        ctx.response.status = 200;
        ctx.set('Content-disposition', 'attachment; filename=' + 'data.xlsx')
        ctx.set('Content-type', 'application/vnd.openxmlformats')
        ctx.set('Content-Length', fileSize)
        ctx.body = buffer
    }

  /**
     * 查询热力图占比数据
     * @param {}} ctx 
     */
    static async getHeatMapPerData(ctx) {
        ctx.response.status = 200;
        ctx.body = statusCode.SUCCESS_200('success', getHeatMapPerData.list)
    }
}
//exports//
module.exports = BuryPointCardController
//exports//